/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.brain.conf.convert;

import com.brain.conf.dto.TerceroDto;
import com.brain.conf.manager.TerceroManager;
import com.brain.core.configuracion.WebServletContextListener;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import org.apache.log4j.Logger;

/**
 *
 * @author Indra
 */
@FacesConverter("terceroConvert")
public class TerceroConvert implements Converter {

    private static Logger logger = Logger.getLogger(TerceroConvert.class);

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        Long id = null;
        try {
            id = Long.valueOf(value);
            if (id == null || value.trim().equals("")) {
                return null;
            } else {
                try {

                    TerceroManager manager = (TerceroManager) WebServletContextListener.getApplicationContext().getBean("terceroManager");
                    return manager.obtenerPorId(id);

                } catch (Exception exception) {
                    logger.error(exception);
                    return null;
                }
            }

        } catch (Exception e) {
            return "";
        }


    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null || value.equals("")) {
            return "";
        } else {
            return String.valueOf(((TerceroDto) value).getId());
        }
    }
}
